package com.lxmes.base.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.base.domain.JcIteminfoOv;
import com.lxmes.common.utils.SecurityUtils;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.service.IJcIteminfoService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
import sun.security.util.SecurityConstants;

/**
 * 物料信息Controller
 *
 * @author ruoyi
 * @date 2022-06-11
 */
@Api(tags="物料管理")
@RestController
@RequestMapping("/base/iteminfo")
public class JcIteminfoController extends BaseController
{
    @Autowired
    private IJcIteminfoService jcIteminfoService;

    /**
     * 查询物料信息列表
     */
    @ApiOperation("获取物料列表")
    @PreAuthorize("@ss.hasPermi('base:iteminfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcIteminfo jcIteminfo)
    {
        startPage();
        jcIteminfo.setDeptId(SecurityUtils.getDeptId());
        List<JcIteminfo> list = jcIteminfoService.selectJcIteminfoList(jcIteminfo);
        return getDataTable(list);
    }

    /**
     * 查询库存信息列表
     */
    @ApiOperation(value="移动端物料列表")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "物料号、物料名称", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = JcIteminfo.class)})
    @GetMapping("/mobileJcIteminfoList")
    public TableDataInfo mobilejcIteminforecordList(JcIteminfo jcIteminfo)
    {
        jcIteminfo.setDeptId(SecurityUtils.getDeptId());
        startPage();
        List<JcIteminfo> list = jcIteminfoService.mobilejcIteminforecordList(jcIteminfo);
        return getDataTable(list);
    }

    @GetMapping(value = "/getItemCode")
    public AjaxResult getItemCode(){
        return AjaxResult.success(jcIteminfoService.getItemAllCode());
    }
    /**
     * 导出物料信息列表
     */
    @PreAuthorize("@ss.hasPermi('base:iteminfo:export')")
    @Log(title = "物料信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcIteminfo jcIteminfo)
    {
        jcIteminfo.setDeptId(SecurityUtils.getDeptId());
        List<JcIteminfo> list = jcIteminfoService.selectJcIteminfoList(jcIteminfo);
        ExcelUtil<JcIteminfo> util = new ExcelUtil<JcIteminfo>(JcIteminfo.class);
        util.exportExcel(response, list, "物料信息数据");
    }

    /**
     * 获取物料信息详细信息
     */
    @ApiOperation("根据ID查询")
    @PreAuthorize("@ss.hasPermi('base:iteminfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jcIteminfoService.selectJcIteminfoById(id));
    }
    /*手机端根据物料条码获取物料信息*/
    @ApiOperation(value="条码号获取物料信息")
    @ApiImplicitParam(name = "tmh", value = "物料条码号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/getJcIteminfo/{tmh}")
    public AjaxResult getJcIteminfo(@PathVariable("tmh")String tmh){
        return AjaxResult.success( (jcIteminfoService.selectJcIteminfoByTmh(tmh)));
    }

    @GetMapping(value = "/getIteminfoByIds/{ids}")
    public AjaxResult getIteminfoByIds(@PathVariable("ids") Long[] ids)
    {
        List<JcIteminfo> list = jcIteminfoService.selecIteminfoByIds(ids);
        return AjaxResult.success(list);
    }

    /**
     * 新增物料信息
     */
    @PreAuthorize("@ss.hasPermi('base:iteminfo:add')")
    @Log(title = "物料信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcIteminfo jcIteminfo)
    {
        return toAjax(jcIteminfoService.insertJcIteminfo(jcIteminfo));
    }
    /**
     * 修改物料信息
     */
    @PreAuthorize("@ss.hasPermi('base:iteminfo:edit')")
    @Log(title = "物料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcIteminfo jcIteminfo)
    {
        return toAjax(jcIteminfoService.updateJcIteminfo(jcIteminfo));
    }

    /**
     * 删除物料信息
     */
    @PreAuthorize("@ss.hasPermi('base:iteminfo:remove')")
    @Log(title = "物料信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(jcIteminfoService.deleteJcIteminfoByIds(ids));
    }




    @Log(title = "物料管理", businessType = BusinessType.IMPORT)
   // @PreAuthorize("@ss.hasPermi('base:iteminfo:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<JcIteminfo> util = new ExcelUtil<JcIteminfo>(JcIteminfo.class);
        List<JcIteminfo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = jcIteminfoService.imporIteminfo(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<JcIteminfo> util = new ExcelUtil<JcIteminfo>(JcIteminfo.class);
        util.importTemplateExcel(response, "物料数据");
    }
    @DeleteMapping("/delAllItemInfo")
    public AjaxResult delAllItemInfo()
    {
        return AjaxResult.success(jcIteminfoService.delAllItemInfo());
    }


}
